﻿using DBUtil.FastEntity.Annotations;
using System;
using System.Collections.Generic;
using System.Text;

namespace DBUtil.FastEntity.Annotations
{
    /// <summary>
    /// 表示将属性映射到列,这一列存储的是json格式数据
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class MapColumnStoreJson : MapColumnAttribute
    {
        public IJsonConverter Converter { get; set; } = DefaultJsonConverter.Instance;
    }

    public interface IJsonConverter
    {
        object DeserializeObject(string text, Type type);

        string SerializeObject(object obj);
    }

    public class DefaultJsonConverter : IJsonConverter
    {
        public static DefaultJsonConverter Instance = new DefaultJsonConverter();
        public object DeserializeObject(string text, Type type)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject(text, type);
        }

        public string SerializeObject(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }
    }
}
